function enterSubmit(event,ourform) {
    if(!event) {
        return true;
    }

    var key_code = event.keyCode ? event.keyCode :
                   event.which ? event.which :
                   void 0;

    if (key_code == 13) {
        ourform.submit();
    } else {
        return true;
    }
}

function getFormElement(form, fieldName) {
    form = typeof form == 'string' ? document[form] : form;

    var found = false;
    for(var i = 0; i < form.elements.length; i++) {
        if(form.elements[i].name == fieldName) {
            found = true;
            break;
        }
    }
    return found ? form.elements[i] : null;
}

function getAllElements() {
    return (document.all) ? document.all : document.getElementsByTagName('*');
}

function collectIDs(prefix, ma_form) {
    ids = new Array();

    var allels = getAllElements();
    for (i = 0; i < allels.length; i++) {
        var elem = allels[i];
        if(elem.type=="checkbox" && elem.name && elem.name.indexOf(prefix) == 0) {
            if(elem.checked) {
                obj_id = elem.name.replace(prefix, '');
                ids.push(obj_id);
            }
        }
    }

    if(ids.length == 0) {
        alert(no_items_selected_message);
        return false;
    } else {
        getFormElement(ma_form, "ids").value = ids.join(',');
        return true;
    }
}

function toggleAll(chunk, checked)
{
    var allels = getAllElements();
    for(var i = 0; i < allels.length; i++) {
        var elem = allels[i];

        if(elem.type=="checkbox" && elem.name && elem.name.indexOf(chunk) >= 0) {
            elem.checked = checked;
        }
    }
}

function toggleDisplay(id) {
    var obj = document.getElementById(id);

    obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
}

function setAll(prefix, value) {
    var allels = getAllElements();
    for (i = 0; i < allels.length; i++) {
        var elem = allels[i];
        if(elem.name && elem.name.indexOf(prefix) == 0) {
            elem.value = value;
        }
    }
}


function getElement(elems, expr) {
     var pattern = new RegExp(expr);
      for (var i = 0; i < elems.length; i++) {
          var elem = elems[i];
          if (elem.id && pattern.test(elem.id))
              return elem;
      }
     return null;
}

function selectChange(name,  lstMain, idChild) {
    var selectsMain = [];
    var i;
    elems = document.getElementsByTagName("select");
    for (i = 0; i < lstMain.length; i++) {
        var elem = getElement(elems, name + ".?" + lstMain[i]);
        if (elem != null) {
            var obj = { str: lstMain[i], element: elem };
            selectsMain.push(obj);
        }
    }

    var selectChild = getElement(elems, name + ".?" + idChild);
    selectChild.selectedIndex = 0;
    for (i = 1; i < selectChild.options.length; i++) {
        var item = selectChild.options[i];
        var correct = true;
        for (var j = 0; j < selectsMain.length && correct; j++) {
            var select = selectsMain[j].element;
            var val = selectsMain[j].str;

            var all = (select.options[select.selectedIndex].value == "");
            correct = correct && (all || (select.options[select.selectedIndex].value == item.getAttribute(val)));
       } 
        
        if (!correct)
            item.style.display = "None";
        else
                item.style.display = null;
    }
}